package com.suyang.syt.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.suyang.syt.dao.interfaces.BannerDao;
import com.suyang.syt.model.Banner;
import com.suyang.syt.util.EmptyUtil;

@Repository
public class BannerDaoImpl extends BaseDaoImpl<Banner> implements BannerDao {

	@Override
	public void deletes(String ids) {
		this.executeByHql("delete Banner where id in(" + ids + ")");
	}

	@Override
	public void setStatus(long id, long objId, int flag) {
		StringBuilder hql = new StringBuilder(" from Banner where flag=? and objId=? ");
		List<Banner> list = this.list(hql.toString(), new Object[] { flag, objId });
		if (EmptyUtil.isNotEmpty(list)) {
			for (Banner banner : list) {
				if (id == banner.getId()) {
					banner.setStatus(1);
				} else {
					banner.setStatus(0);
				}
				this.update(banner);
			}
		}
	}

	@Override
	public long countBannersBy(int flag, long objId) {
		String hql = " select count(*) from Banner b where b.flag=? and b.objId=? ";
		return (Long) this.queryByHql(hql, new Object[] { flag, objId });
	}

	@Override
	public List<Banner> findByObjIds(int flag, String ids) {
		String hql = " from Banner b where b.flag=" + flag + " and id in(" + ids + ")";
		return this.list(hql);
	}

}
